import scrapy
from dangdang.items import DangdangItem

class DangSpider(scrapy.Spider):
    name = "dang"
    allowed_domains = ["category.dangdang.com"]
    start_urls = ["https://category.dangdang.com/cp01.54.06.19.00.00.html"]
    page = 1
    def parse(self, response):
        # src = //ul[@id="component_59"]//li//img/@src
#       alt = //ul[@id="component_59"]//li//img/@alt
#       price = //ul[@id="component_59"]//li//p[@class="price"]/span[1]/text()
        li_list = response.xpath('//ul[@id="component_59"]//li')
        for li in li_list:
            src = li.xpath('.//img/@data-original').extract_first()
            if src:
                src = src
            else:
                src = li.xpath('.//img/@src').extract_first()
            name = li.xpath('.//img/@alt').extract_first()
            price = li.xpath('.//p[@class="price"]/span[1]/text()').extract_first()
            # print(src,name,price)
            book = DangdangItem(src=src,name=name,price=price)
            # book = DangdangItem(src=src,name=name,price=price)
            yield book
        if self.page < 100:
            self.page = self.page + 1
            url = f'https://category.dangdang.com/pg{self.page}-cp01.54.06.19.00.00.html'
            yield scrapy.Request(url=url, callback=self.parse)